package org.soulraven.games.memory.ui.config;

import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soulraven.games.memory.model.GameMode;
import org.soulraven.games.memory.model.Player;
import org.soulraven.games.memory.util.MessageUtil;

public class TwoPlayersPanel extends JPanel implements ConfigPanel {

	private static final long serialVersionUID = 2600722636435749186L;

	private JTextField playerOneNameTextField = null;
	private JTextField playerTwoNameTextField = null;

	public TwoPlayersPanel() {
		this.setLayout(new GridLayout(2, 2, 5, 5));
		this.add(new JLabel(MessageUtil.getMessage("playeronename")));
		this.add(getPlayerOneNameTextField());
		this.add(new JLabel(MessageUtil.getMessage("playertwoname")));
		this.add(getPlayerTwoNameTextField());
	}

	/**
	 * @return the playerOneNameTextField
	 */
	private JTextField getPlayerOneNameTextField() {
		if (playerOneNameTextField == null) {
			playerOneNameTextField = new JTextField();
			playerOneNameTextField.setSize(120, 20);
			playerOneNameTextField.setPreferredSize(playerOneNameTextField.getSize());
		}
		return playerOneNameTextField;
	}

	/**
	 * @return the playerTwoNameTextField
	 */
	private JTextField getPlayerTwoNameTextField() {
		if (playerTwoNameTextField == null) {
			playerTwoNameTextField = new JTextField();
			playerTwoNameTextField.setSize(120, 20);
			playerTwoNameTextField.setPreferredSize(playerTwoNameTextField.getSize());
		}
		return playerTwoNameTextField;
	}

	/**
	 * @return the playerOneName
	 */
	public String getPlayerOneName() {
		return getPlayerOneNameTextField().getText().trim();
	}

	/**
	 * @return the playerTwoName
	 */
	public String getPlayerTwoName() {
		return getPlayerTwoNameTextField().getText().trim();
	}

	@Override
	public GameMode getGameMode() {
		return GameMode.TWO_PLAYERS_MODE;
	}

	@Override
	public boolean isDataEntered() {
		return (!"".equals(getPlayerOneName()) && !"".equals(getPlayerTwoName()));
	}

	@Override
	public void showWarning(final Component parent) {
		String message = MessageUtil.getMessage("emptyplayersnames");
		String title = MessageUtil.getMessage("warning");
		JOptionPane.showMessageDialog(parent, message, title,
				JOptionPane.WARNING_MESSAGE);
	}

	@Override
	public List<Player> getPlayers() {
		List<Player> players = new ArrayList<Player>();
		players.add(new Player(1, getPlayerOneName()));
		players.add(new Player(2, getPlayerTwoName()));
		return players;
	}
}
